package network

import (
	"context"
	//	"encoding/json"
	"fmt"

	. "gitee.com/liukexing/isrn/api"
)

/*
USAGE
  ipfs id [<peerid>] - Show ipfs node id info.

SYNOPSIS
  ipfs id [--format=<format> | -f] [--] [<peerid>]

ARGUMENTS

  [<peerid>] - Peer.ID of node to look up.

OPTIONS

  -f, --format string - Optional output format.

DESCRIPTION

  Prints out information about the specified peer.
  If no peer is specified, prints out information for local peers.

  'ipfs id' supports the format option for output with the following keys:
  <id> : The peers id.
  <aver>: Agent version.
  <pver>: Protocol version.
  <pubkey>: Public key.
  <addrs>: Addresses (newline delimited).

  EXAMPLE:

      ipfs id Qmece2RkXhsKe5CRooNisBTh4SK119KrXXGmoK6V3kb8aH -f="<addrs>\n"
*/

type IDCommand struct {
	Shell
}

type IdOutput struct {
	ID              string
	PublicKey       string
	PrivateKey      string
	Addresses       []string
	AgentVersion    string
	ProtocolVersion string
}

func (s *IDCommand) ID(peer ...string) (*IdOutput, error) {
	if len(peer) > 1 {
		return nil, fmt.Errorf("Too many peer arguments")
	}
	out := &IdOutput{}
	err := s.Request("id", peer...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	out.PrivateKey = s.GetPrivateKey(out.ID)
	return out, nil
	//	resp, err := s.NewRequest(context.Background(), "id", peer...).Send(s.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}

	//	defer resp.Close()
	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	decoder := json.NewDecoder(resp.Output)
	//	out := new(IdOutput)
	//	err = decoder.Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}
	//	out.PrivateKey = s.GetPrivateKey(out.ID)
	//	return out, nil
}
